#!/bin/sh
#desc:Control background music
#package:odbgmusic
#type:local

VOLUME=100
CONFIGFILE="/etc/opendomo/music.conf"
if test -f $CONFIGFILE; then
	. $CONFIGFILE
fi
VOL="0,10,20,30,40,50,60,70,80,90,100"
IFS=$'\x0A'$'\x0D'

# Is necessary configure player before 

if ! test -e $CONFIGFILE; then
	echo "form:`basename $0`"
	echo "#WARN odMusic not configured"
	echo "actions:"
	echo "	bgmusicConfig.sh	Configure music player"
	echo
	exit
fi

. $CONFIGFILE

# If player is in execution, show information of play

if test -e $PID; then
	
	# Configure variables for web interface

	. $PID
	SONGROUTE=`cat $TMPCURRENT`
	SONG=`basename $SONGROUTE` 
	COVER=`echo $COVER | sed "s/\/media\///g"`

	# Clean temporal control parameters

	rm $TMPCTLPARM
	/usr/local/opendomo/bgmusicVolume.sh $VOLUME

	# Change volume level

	if ! test -z "$1"; then
		if [ $1 -eq $1 ]; then
			VOLUME="$1"
			/usr/local/opendomo/bgmusicVolume.sh $VOLUME
		fi
	fi	

	# Web interface

	echo "#> Playing now..."
	echo "list:`basename $0`	iconlist"
	echo "	-$COVER	$ARTIST - $ALBUM ($SONG)	file image"
	echo "actions:"
	echo "	bgmusicPrev.sh	<<<<"
	echo "	bgmusicStop.sh	Stop"
	echo "	bgmusicPause.sh	Pause"
	echo "	bgmusicNext.sh	>>>>"
	echo
	echo "#> Playing options"
	echo "form:`basename $0`"
	echo "	VOLUME	Volume level	subcommand[$VOL]	$VOLUME"
	echo
	
else
	# If not playing, select to play interface	
	. $CONFIGFILE

	# Check parameter is a option, artist filter or volume level
	case $1 in
		VOLUME)
                        cat "$TMPCTLPARM" | grep -v VOLUME > $TMPCTLPARM
			echo "VOLUME=$2"        	>>$TMPCTLPARM
		;;
		RDM)
                        cat "$TMPCTLPARM" | grep -v RDM > $TMPCTLPARM
			echo "RDM=$2"        	>>$TMPCTLPARM
		;;
		LOOP)
                        cat "$TMPCTLPARM" | grep -v LOOP > $TMPCTLPARM
			echo "LOOP=$2"        	>>$TMPCTLPARM
		;;
		OPTION)
                        cat "$TMPCTLPARM" | grep -v ARTIST > $TMPCTLPARM
			ARTIST=`echo $2 | tr + " "`
			echo "ARTIST=\"$ARTIST\"" 	>>$TMPCTLPARM
		;;		
	esac
	
	# Update option parameters	

	if test -f $TMPCTLPARM; then
		. $TMPCTLPARM
		chown admin:admin 	  $TMPCTLPARM
		chmod a+rw 		  $TMPCTLPARM
	fi

	# Show web interface, albums and options

	# List of artist in database

	for artist in `cat $MUSICDB | grep cover | cut -f2 -d"\"" | sort -u`; do

		if test "$artist"; then
			if test -z $ARTLIST; then
				ARTLIST="$artist"
			else
				ARTLIST="$ARTLIST","$artist"
			fi
		fi
	done
				
	# If artist isn't select, select last
	if test -z $ARTIST; then 
		ARTIST=$artist
	fi
	
	ALBUMS=`cat $MUSICDB | grep cover | grep $ARTIST | cut -f2 -d"(" | cut -f1 -d")"`	

	echo "#> Filter by artist"
	echo "form:`basename $0`"
	echo "	OPTION	Options param	hidden	OPTION"
	echo "	ARTIST	Select artist	list[$ARTLIST]	$2"		
	echo "	VOLUME	Volume level	subcommand[$VOL]	$VOLUME"
	echo "	RDM	Random	subcommand[on,off]	$RDM"
	echo "	LOOP	Continuos mode	subcommand[on,off]	$LOOP"
	echo

	echo "#> Select album"
	echo "list:`basename $0`	iconlist"
	
	for album in $ALBUMS; do
		DBCOVER=`cat $MUSICDB | grep "($album)" | grep "$artist" | grep cover | cut -f4 -d"\"" | sort -u`
		COVER=`echo $DBCOVER | sed "s/\/media\///g"`
		SORTNAME=`echo ${album:0:35}`

		echo "	$COVER	$SORTNAME	file image"
	done

	echo "actions:"
	echo "	bgmusicPlay.sh	Play music"	
	echo
fi

